const express = require('express');
const pool = require('../../pool.js');
const user = express.Router();
// 登录信息核对
user.post('/login', (req, res) => {
    pool.query('select * from user where user=? and pwd=? and roles=?', [req.body.user, req.body.pwd, req.body.roles], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
user.post('/register', (req, res) => {
    let { user, pwd, email, user_name, shopid } = req.body;
    let roles = '商家';
    pool.query('insert user set user=?,pwd=?,email=?,roles=?,user_name=?,shopId=?', [user, pwd, email, roles, user_name, shopid], (err, r) => {
        if (err) {
            res.send({ msg: '该账号已被注册' });
        }
        res.send(r);
    })
})
user.put('/repwd', (req, res) => {
    let pwd = req.body.pwd;
    let email = req.body.email;
    pool.query('update user set pwd=? where email=?', [pwd, email], (err, r) => {
        if (err) {
            res.send({ msg: '密码不能和以前一致' });
        } else {
            res.send({ msg: '修改成功!' });
        }
    })
})
// 订单详情页显示用户信息
user.get('/customer', (req, res) => {
    let uname = req.query.user;
    pool.query('select * from customer where uname = ?', [uname], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})

module.exports = user;